package ru.papergames.view.mediators
{
	import mx.utils.URLUtil;
	import org.puremvc.as3.interfaces.INotification;
	import ru.papergames.controller.notifications.FrontendNotifications;
	import ru.papergames.model.proxies.LoginProxy;
	import ru.papergames.utils.constants.ConfigKeys;
	import ru.papergames.utils.constants.PersistentKeys;
	import ru.papergames.utils.constants.TopViews;
	import ru.papergames.view.events.LoginEvent;
	import ru.papergames.view.screens.LoginTopView;
	
	
	public class LoginTopViewMediator extends BaseMediator
	{
		
		public static const NAME:String = 'LoginTopViewMediator';
		
		public function LoginTopViewMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void {
			super.onRegister();
						
			var storedLVO:Object = this.persistentProxy.retrieveData(PersistentKeys.LOGIN_DATA);			
			if (storedLVO && storedLVO.username) {
				view.login.text = storedLVO.username;
				view.pass.text = storedLVO.password;
				view.saveLogin.selected = true;
			}
			
			addListeners();
		}
		
		override public function onRemove():void {
			super.onRemove();
			removeListeners();
			
			//clean fields
			//view.captcha_code.text = '';
			view.loginPanel.status = '';
			
			//loginModule.loginVO = null;
			
			this.setViewComponent(null);
		}
		
		protected function get view():LoginTopView {
			return this.viewComponent as LoginTopView;
		}		
		
		override public function listNotificationInterests():Array {
			var notes:Array = super.listNotificationInterests();
			
			notes.push( LoginProxy.LOGIN_SUCCESS,
						LoginProxy.LOGIN_FAULT );
			
			return notes;
		}
		
		override public function handleNotification(note:INotification):void {
			var captchaToReload:Boolean = false;
			
			view.tryLoginButton.enabled = true;
			view.goRegButton.enabled = true;
			//view.forgotPassButton.enabled = true;
			view.saveLogin.enabled = true;
			
			switch(note.getName()) {				
				case LoginProxy.LOGIN_SUCCESS:
					view.loginPanel.status = 'Успешный вход';
					
					//check remember					
					this.persistentProxy.storeData(PersistentKeys.LOGIN_DATA, 
						(loginProxy.loginVO.isLoggedIn && loginProxy.loginVO.rememberMe) ? loginProxy.loginVO : null);
					
					
					facade.sendNotification(FrontendNotifications.SWITCH_TOP_VIEW, 
						loginProxy.loginVO.needPersCreation ? TopViews.PERS_CREATION : TopViews.GAME_UI);
						
										
					break;
					
				case LoginProxy.LOGIN_FAULT:
					view.loginPanel.status = 'Ошибка входа';
					captchaToReload = true;
					break;
				
			}			
			
			
		}
		
		
		/*** PRIVATE IMPLEMENTATION ********/
		
		private function addListeners():void {
			view.addEventListener(LoginEvent.TRY_LOGIN, onTryLogin);
			view.addEventListener(LoginEvent.GO_REGISTRATION, onGoRegistration);
			view.addEventListener(LoginEvent.REMIND_PASS, onPassRemind);
		}
		
		private function removeListeners():void {
			view.removeEventListener(LoginEvent.TRY_LOGIN, onTryLogin);
			view.removeEventListener(LoginEvent.GO_REGISTRATION, onGoRegistration);
			view.removeEventListener(LoginEvent.REMIND_PASS, onPassRemind);
		}
		
		private function onGoRegistration(evt:LoginEvent):void {
			facade.sendNotification(FrontendNotifications.GO_TO_REGISTRATION);
		}
		
		private function onTryLogin(evt:LoginEvent):void {
			view.loginPanel.status = '';
			view.tryLoginButton.enabled = false;
			view.goRegButton.enabled = false;
			//view.forgotPassButton.enabled = false;
			view.saveLogin.enabled = false;
			
			facade.sendNotification(FrontendNotifications.TRY_LOGIN, evt.loginVO);
		}
		
		private function onPassRemind(evt:LoginEvent):void {
			
		}
	}
}